package com.sdkx.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdkx.app.R;

import android.widget.ListView;
import android.widget.ToggleButton;

import com.sdkx.app.model.CurrentAirWay;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.view.ClearEditText;
import com.sdkx.app.view.SideBar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

//航线编辑
public class RoutePointAdapter extends BaseAdapter {
    private List<WayPoint> wayPoints = new ArrayList<>();

    private int curselect = -1;
    private Context context;
    private LayoutInflater layoutInflater;

    public RoutePointAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setWayPoints(List<WayPoint> points) {
        wayPoints = points;
        notifyDataSetChanged();
    }

    public void swapUp() {
        if (curselect > 0) {
            Collections.swap(wayPoints, curselect - 1, curselect);
            curselect--;
            notifyDataSetChanged();
        }
    }

    public void swapDown() {
        if (curselect >= 0 && curselect + 1 < wayPoints.size()) {
            Collections.swap(wayPoints, curselect, ++curselect);
            notifyDataSetChanged();
        }
    }

    public void reverse() {
        curselect = -1;
        Collections.reverse(wayPoints);
        notifyDataSetChanged();
    }

    public void clearSelect() {
        curselect = -1;
        notifyDataSetChanged();
    }
    public void delete(){
        if(curselect >= 0 && wayPoints.size() > curselect){
            wayPoints.remove(curselect);
            notifyDataSetChanged();
        }
    }

    public void clearAll(){
        wayPoints.clear();
        notifyDataSetChanged();
    }
    public List<WayPoint> getWayPoints(){
        return wayPoints;
    }

    @Override
    public int getCount() {
        return wayPoints.size();
    }

    @Override
    public WayPoint getItem(int position) {
        return wayPoints.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.wplist_item, null);
            holder = new ViewHolder();
            holder.img = convertView.findViewById(R.id.iv_three_menu_icon);
            holder.tv_wpname = convertView.findViewById(R.id.tv_wpname);
            holder.mainmenu = convertView.findViewById(R.id.mainmenu);
            convertView.setTag(holder);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    curselect = position;
                    v.findViewById(R.id.mainmenu).setBackgroundColor(Color.parseColor("#FF0000"));
                    notifyDataSetChanged();
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        WayPoint point = wayPoints.get(position);
        holder.tv_wpname.setText(point.getName());
        if (curselect == position) {
            holder.mainmenu.setBackgroundColor(Color.parseColor("#FF0000"));
        } else {
            holder.mainmenu.setBackgroundColor(Color.parseColor("#00000000"));
        }
        return convertView;
    }


    protected class ViewHolder {
        TextView tv_wpname;
        ImageView img;
        View mainmenu;
    }
}
